package com.canopys.study.open.test.leetcode;

/**
 * @Author: Canopys
 * @Descrption:
 * @Date: Created in 22:34 2020/12/19
 * @Modified By:
 */
public class Fanzhuan {

    public static void rotate(int[][] matrix) {
        if (matrix == null) {
            return;
        }

        int length = matrix[0].length;
        for (int i = 0; i <= length/2; i++) {
            for (int j = i; j < length - i - 1; j++) {
                //int [] tmp = new int[length - 2 * i];
                //int pre = matrix[i][length - i];
                //matrix[i][length - i - 1] = matrix[i][j];
                /*int pre = matrix[j][length - i - 1];
                matrix[j][length - i - 1] = matrix[i][j];

                int next = matrix[length - j - 1][length - i - 1];
                matrix[length - j - 1][length - i - 1]  = pre;
                pre = next;

                next = matrix[length - j - 1][i];
                matrix[length - j - 1][i] = pre;
                pre = next;

                //next = matrix[i][j];
                matrix[i][j] = pre;*/

                int pre = matrix[i][j];
                int next = matrix[j][length - 1 - i];
                matrix[j][length - 1 - i] = pre;
                pre = next;

                next = matrix[length - 1 - i][length - 1 - j];
                matrix[length - 1 - i][length - 1 - j] = pre;
                pre = next;

                next = matrix[length - 1 - j][i];
                matrix[length - 1 - j][i] = pre;
                pre = next;

                next = matrix[i][j];
                matrix[i][j] = pre;
                pre = next;

            }
        }

    }

    public static void main(String[] args) {
        //int[][] matrix = new int[][] {{ 5, 1, 9,11},{2, 4, 8,10},{13, 3, 6, 7},{15,14,12,16}};
        int[][] matrix = new int[][] {{ 1,2,3},{4,5,6},{7,8,9}};

        rotate(matrix);
        System.out.println(matrix);

        StringBuilder sb = new StringBuilder();
        //sb.toString().concat()
    }
}
